//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package com.openbravo.pos.panels;

import java.util.List;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.ListQBFModelNumber;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.ui.QBFCompareEnumListCellRenderer;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.pos.base.AppLocal;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.bean.ProductCategory;

public class ProductFilterSales extends javax.swing.JPanel implements EditorCreator {
	private ComboBoxValModel m_CategoryModel;
    I_DataLogicSales dlSales;

    /** Creates new form ProductFilterSales */
    public ProductFilterSales(I_DataLogicSales dlSales, JEditorKeys jKeys) {
        initComponents();

        this.dlSales=dlSales;

        m_CategoryModel = new ComboBoxValModel();

        m_jCboPriceSell.setModel(ListQBFModelNumber.getMandatoryNumber());
        m_jCboPriceSell.setRenderer(new QBFCompareEnumListCellRenderer());
        m_jPriceSell.addEditorKeys(jKeys);

        m_jtxtName.addEditorKeys(jKeys);

        m_jtxtBarCode.addEditorKeys(jKeys);
    }

    public void activate() {

        m_jtxtBarCode.reset();
        //m_jtxtBarCode.setEditModeEnum(JEditorString.MODE_123);
        m_jtxtName.reset();
        m_jPriceSell.reset();
         m_jtxtBarCode.activate();

        try {
            List catlist = dlSales.list(ProductCategory.class);
            catlist.add(0, null);
            m_CategoryModel = new ComboBoxValModel(catlist);
            m_jCategory.setModel(m_CategoryModel);
        } catch (BasicException eD) {
            // no hay validacion
        }
    }

    public Object createValue() throws BasicException {

        Object[] afilter = new Object[14];

        // Nombre
        if (m_jtxtName.getText() == null || m_jtxtName.getText().equals("")) {
            afilter[0] = QBFCompareEnum.COMP_NONE;
            afilter[1] = null;
        } else {
            afilter[0] = QBFCompareEnum.COMP_RE;
            afilter[1] = "%" + m_jtxtName.getText() + "%";
        }

        // Precio de compra
        afilter[2]  = QBFCompareEnum.COMP_NONE;
        afilter[3] = null;

        // Precio de venta
        afilter[5] = m_jPriceSell.getDoubleValue();
        afilter[4] = afilter[5] == null ? QBFCompareEnum.COMP_NONE : m_jCboPriceSell.getSelectedItem();

        // Categoria
        if (m_CategoryModel.getSelectedKey() == null) {
            afilter[6] = QBFCompareEnum.COMP_NONE;
            afilter[7] = null;
        } else {
            afilter[6] = QBFCompareEnum.COMP_EQUALS;
            afilter[7] = m_CategoryModel.getSelectedKey();
        }

        // el codigo de barras
        if (m_jtxtBarCode.getText() == null || m_jtxtBarCode.getText().equals("")) {
            afilter[8] = QBFCompareEnum.COMP_NONE;
            afilter[9] = null;
        } else{
            afilter[8] = QBFCompareEnum.COMP_RE;
            afilter[9] = "%" + m_jtxtBarCode.getText() + "%";
        }

        // Date range

        afilter[10] = QBFCompareEnum.COMP_NONE ;
        afilter[11] = null;
        afilter[12] =  QBFCompareEnum.COMP_NONE ;
        afilter[13] = null;
        
        return afilter;
    }

    public void reset(){
        m_jtxtBarCode.reset();
        m_jtxtBarCode.activate();
    };

    public boolean isBarCodeEmpty(){
        return m_jtxtBarCode.getText()==null||m_jtxtBarCode.getText().isEmpty();
    }

    public String getBarCode(){
        return m_jtxtBarCode.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        m_jtxtName = new com.openbravo.editor.JEditorString();
        jLabel2 = new javax.swing.JLabel();
        m_jCategory = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        m_jCboPriceSell = new javax.swing.JComboBox();
        m_jPriceSell = new com.openbravo.editor.JEditorCurrency();
        m_jtxtBarCode = new com.openbravo.editor.JEditorString();
        jLabel1 = new javax.swing.JLabel();
        jSearchCategory = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(370, 170));
        setLayout(null);

        jLabel5.setText(AppLocal.getIntString("label.prodname")); // NOI18N
        add(jLabel5);
        jLabel5.setBounds(20, 40, 110, 15);
        add(m_jtxtName);
        m_jtxtName.setBounds(130, 40, 290, 25);

        jLabel2.setText(AppLocal.getIntString("label.prodcategory")); // NOI18N
        add(jLabel2);
        jLabel2.setBounds(20, 70, 110, 15);
        add(m_jCategory);
        m_jCategory.setBounds(130, 70, 230, 21);

        jLabel3.setText(AppLocal.getIntString("label.prodpricesell")); // NOI18N
        add(jLabel3);
        jLabel3.setBounds(20, 130, 110, 15);
        add(m_jCboPriceSell);
        m_jCboPriceSell.setBounds(130, 130, 150, 24);
        add(m_jPriceSell);
        m_jPriceSell.setBounds(290, 130, 130, 25);
        add(m_jtxtBarCode);
        m_jtxtBarCode.setBounds(130, 10, 290, 25);

        jLabel1.setText(AppLocal.getIntString("label.prodbarcode")); // NOI18N
        add(jLabel1);
        jLabel1.setBounds(20, 10, 110, 15);

        jSearchCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
        jSearchCategory.setFocusable(false);
        jSearchCategory.setPreferredSize(new java.awt.Dimension(35, 25));
        jSearchCategory.setRequestFocusEnabled(false);
        jSearchCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchCategoryActionPerformed(evt);
            }
        });
        add(jSearchCategory);
        
        m_jCategory.setFocusable(false);
        jSearchCategory.setBounds(360, 70, 30, 20);
    }// </editor-fold>


     private void jSearchCategoryActionPerformed(java.awt.event.ActionEvent evt) {
        JCategoryTreeDialog dailog=JCategoryTreeDialog.newJDialog(this);
        dailog.showDialog(m_CategoryModel.getDatas(),(ProductCategory) m_CategoryModel.getSelectedItem());
        if(dailog.isOK())
            m_CategoryModel.setSelectedItem(dailog.getSelected());
}

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jSearchCategory;
    private javax.swing.JComboBox m_jCategory;
    private javax.swing.JComboBox m_jCboPriceSell;
    private com.openbravo.editor.JEditorCurrency m_jPriceSell;
    private com.openbravo.editor.JEditorString m_jtxtBarCode;
    private com.openbravo.editor.JEditorString m_jtxtName;
    // End of variables declaration

}
